package org.qdog.pk.pkscrape.entity;

import org.apache.commons.lang.StringUtils;

/**
 * @since 08/2011
 */
public enum EntityType {
  City(City.class, 24 * 2) {
    @Override
    public Entity internalCreateEntity(final String entityId) {
      return new City(entityId);
    }
  },
  Person(Person.class, 24 * 7) {
    @Override
    public Entity internalCreateEntity(final String entityId) {
      return new Person(entityId);
    }

  },
  Kingdom(Kingdom.class, 24) {
    @Override
    public Entity internalCreateEntity(final String entityId) {
      return new Kingdom(entityId);
    }

  };

  private int staleHours;
  private Class<? extends Entity> entityClass;

  private EntityType(Class<? extends Entity> entityClass, int staleHours) {
    this.staleHours = staleHours;
    this.entityClass = entityClass;
  }

  protected abstract Entity internalCreateEntity(final String entityId);

  public Class<? extends Entity> getEntityClass() {
    return entityClass;
  }
  public int getStaleHours() {
    return staleHours;
  }

  public static Entity createEntity(String entityId) {
    return entityTypeFromId(entityId).internalCreateEntity(entityId);
  }

  public static EntityType entityTypeFromId(String id) {
    EntityType result = null;
    if (StringUtils.startsWith(id, "Chr")) {
      result = EntityType.Person;
    } else if (StringUtils.startsWith(id, "Cty")) {
      result = EntityType.City;
    } else if (StringUtils.startsWith(id, "Kng")) {
      result = EntityType.Kingdom;
    } else {
      throw new RuntimeException("Could not determine entity type from id '" + id + "'");
    }

    return result;
  }


}
